# -*- coding: utf-8 -*-
"""
@Time：YourTime
@Author  : jiahuifeng
@Desc    : 接口平台登录接口
"""

from fastapi import APIRouter, status, HTTPException
from pydantic import BaseModel, Field

from orm.models import User_Register
# from passlib.context import CryptContext

api_login = APIRouter()


class User_Info(BaseModel):
    username: str = Field(..., title="用户名", description="用户名", max_length=20)
    password: str = Field(..., title="密码", description="密码", max_length=20)


# 创建密码哈希上下文
# pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")


@api_login.post('/login', status_code=status.HTTP_200_OK)
async def user_login(user: User_Info):
    user_existing = await User_Register.filter(username=user.username).first()
    if not user_existing:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail="用户名不存在，请重新输入")
    if user.password != user_existing.password:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail="密码错误，请重新输入")
    return {
        "status": status.HTTP_200_OK,
        "message": "success",
        "data": {
            "username": user.username,
            "token": "1234567890"
        }
    }
